package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.setmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImp implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private setmealDishMapper setmealDishMapper;


    @Override
    public void saveWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
//        dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            for(DishFlavor flavor : flavors){
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(queryDTO);
        long total = page.getTotal();
        List<DishVO> records = page.getResult();
        return new PageResult(total,records);

    }

    @Override
    @Transactional()
    public void deleteDish(List<Long> ids) {
        //1.遍历整个批量删除并查询状态 0停售，1起售，1时不能删除，当有一个不能删除时，整个操作都不能删除
        for(Long id : ids){
           Dish dish = dishMapper.getbyId(id);
            if(dish.getStatus() == StatusConstant.ENABLE)
            {throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //2.查询是否整个批量删除中是否关联了套餐
            List<Long> setmealIds = setmealDishMapper.setmealIds(ids);
            if(setmealIds != null && setmealIds.size() > 0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
        //3.删除dish //4.删除dish_favoor
//        for(Long id: ids){
//            dishMapper.deleteById(id);
//            dishFlavorMapper.deleteBydishId(id);
//        }

        //批量删除
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteBydishIds(ids);

    }

    @Override
    public DishVO getById(Long id) {
         Dish dish = new Dish();
         dish = dishMapper.getbyId(id);
         List<DishFlavor> flavors = dishFlavorMapper.getbydishId(id);
         DishVO dishVO = new DishVO();
         BeanUtils.copyProperties(dish,dishVO);
         dishVO.setFlavors(flavors);

        return dishVO;
    }

    @Override
    public void updata(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updata(dish);
        dishFlavorMapper.deleteBydishId(dishDTO.getId());

        List<DishFlavor> dishFlavors = dishDTO.getFlavors();

        if(dishFlavors != null && dishFlavors.size() > 0){
            for(DishFlavor flavor : dishFlavors){
                flavor.setDishId(dishDTO.getId());

            }
            dishFlavorMapper.insertBatch(dishFlavors);
        }

    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish = dishMapper.getbyId(id);
        dish.setStatus(status);
        dishMapper.updata(dish);
    }

    @Override
    public List<Dish> getdishBylist(Long id) {
        Dish dish = Dish.builder()
                .categoryId(id)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }


    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getbydishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
